  window.onload = function() {
  	layui.use(['element', 'form', 'laydate', 'jquery', 'laypage'], function() {
  		var element = layui.element,
  		form = layui.form,
  		laydate = layui.laydate,
  		$ = layui.jquery,
  		laypage = layui.laypage;

  		var addActive = {
  			at_name: "",
  			b_time:"",
  			e_time:"",
  			join_num:"",
  			id:"",
  			step:1,
  			step1:$('.step1'),
  			step2:$('.step2'),
  			step3:$('.step3'),
  			nextBtn:$(".next-pt"),
  			prevBtn:$(".prev-pt"),
  			goodsList:$('.goods_list'),
  			skuTbody:$("#sku_tbody"),
        searchKey:'',
  			next:function(){
  				var that = this;
  				that.nextBtn.on("click", function() {
  					if(that.step == 1){
  						that.at_name = $(".at_name").val();
  						that.b_time = $(".b-time").val();
  						that.e_time = $(".e-time").val();
  						that.join_num = $(".join_num").val();
  						if (that.at_name == "" || that.b_time == "" || that.e_time == "" || that.join_num == "") {
  							Utils.layer.msg("请完善信息");
  							return;
  						} else if (that.at_name.length > 45) {
  							Utils.layer.msg("名称不能超过45个字")
  							return;
  						} else if (that.join_num < 2 || that.join_num > 10) {
  							Utils.layer.msg("请输入2~10人")
  							return;
  						} else if (that.b_time + ' 00:00:00' >= that.e_time + ' 23:59:59') {
  							Utils.layer.msg("结束的时间需大于开始时间")
  							return;
  						}else{
  							that.showGoodsList("",1);
                
  						}
	  				}else if(that.step == 3){
	  					var is_checked = $(".choose-gd tbody input[type='checkbox']:checked");
	  					var len = is_checked.length;
	  					if(len < 1){
	  						Utils.layer.msg('请选择属性');
	  						return;
	  					}
	  					var cndsss = $(".cnds").val();
	  					var Th_tbody = document.getElementById("Th_tbody");
	  					$("#Th_tbody").empty();
	  					for (var i = 0; i < len; i++) {
	  						var attr_value = $("tbody input[type='checkbox']:checked").eq(i).attr("attr_value");
	  						var attr_img = $("tbody input[type='checkbox']:checked").eq(i).attr("attr_img");
	  						var original_price = $("tbody input[type='checkbox']:checked").eq(i).attr("original_price");
	  						var discount_price = $("tbody input[type='checkbox']:checked").eq(i).attr("discount_price");
	  						var inventory = $("tbody input[type='checkbox']:checked").eq(i).attr("inventory");
	  						var attribute_value_id = $("tbody input[type='checkbox']:checked").eq(i).attr("attribute_value_id");
	  						var attr_id = $("tbody input[type='checkbox']:checked").eq(i).attr("id");
                var imgCdn = $("tbody input[type='checkbox']:checked").eq(i).attr("cdn");

                var str1 = attr_value ? '<td><input type="text" class="no_width deep layui-input" value="' + attr_value + '" disabled></td>' : '<td style="display:none;"><input type="text" class="no_width deep layui-input" value="' + attr_value + '" disabled></td>';
                if(!attr_value){
                  $('.choose-gd thead tr th.sku').hide()
                }else{
                  $('.choose-gd thead tr th.sku').show()
                }

	  						str1 += '<td><input type="hidden" class="no_width deep layui-input" value="' + attr_img + '"><img src="' + imgCdn + attr_img + '"/></td>'
	  						str1 += '<td><input type="text" class="no_width shallow layui-input" value="' + discount_price + '" placeholder="请输入单购价"></td>'
	  						// str1 += '<td><input type="text" class="no_width shallow layui-input" value="' + one_price + '"></td>'
	  						str1 += '<td><input type="text" class="no_width shallow layui-input" value="' + original_price + '"></td>'
	  						str1 += '<td><input type="text" class="no_width shallow layui-input" value="' + inventory + '"></td>'
	  						str1 += '<td style="display: none"><input type="hidden" value="' + attribute_value_id + '"></td>'
	  						str1 += '<td style="display: none"><input type="hidden" value="' + attr_id + '"></td>'

	  						var Tr = document.createElement("tr");
	  						Tr.innerHTML = str1;
	  						Th_tbody.appendChild(Tr)
	  					}
	  				}
	  				that.step = that.step + 1;
		  			that.showStepItem(that.step);
		  			that.showStep();
	  			});
  			},
  			prev:function(){
  				var that = this;
  				that.prevBtn.on("click", function() {
  					that.step = that.step - 1;
  					that.showStepItem(that.step);
  					that.showStep();
  				})
  			},
  			search:function(){
  				var that = this;
  				$('.search_btn').click(function(){
  					that.searchKey = $('.search_input').val();
  					that.showGoodsList(that.searchKey, 1)
  				})
  			},
  			showGoodsList:function( key, page){
  				var that = this;
          Utils.layer.load(1)
  				$.get("/active/search-goods", { goods_name: key, p:page }, function success(data) {
            Utils.layer.closeAll()
  					if (data.code == 200) {
  						that.goodsList.empty();
  						var res = data.data.lists;
  						var strli = '';
  						var listIdx = '';
  						for (var i = 0; i < res.length; i++) {
  							strli += '<li class="li xyb1" data-id="' + res[i].id + '">' + res[i].goods_name + '</li>';
  						}
  						that.goodsList.html(strli);
							//执行一个laypage实例
							laypage.render({
								elem: 'goodsListPage',
								count: data.data.count.total_count,
								curr:data.data.count.current,
                jump: function(obj, first){
                    //首次不执行
                    if(!first){
                      console.log(obj.curr);
                      that.showGoodsList(that.searchKey, obj.curr)
                    }
                  }
							});
							that.showSku();
						}
					});
  			},
  			showSku:function(){
  				var that = this;
  				that.goodsList.find('li').unbind('click').click(function(){
  					that.id = $(this).attr("data-id");
  					that.step = 3;
  					that.showStep();
  					that.skuTbody.empty();
  					$.post("/active/getsku", { id: $(this).attr("data-id") },function success(data) {
  						if (data.code == 200) {
                console.log(data)
  							dIv = '<thead><tr>';
  							dIv += '<th width="40"><input type="checkbox" class="checkbox total" status="off"></th>';
  							$(data.data.goods_attr.thead).each(function(i, item) {
  								dIv += '<th>' + item + '</th>'
  							})
  							dIv += '<th>拼团价</th>';
  							// dIv += '<th>单购价</th>';
  							dIv += '<th>原价</th>';
  							dIv += '<th>库存</th>';
  							dIv += '</tr></thead>';
  							dIv += '<tbody id="t_b">';
  							// " one_price="' + item.one_price + '
  							$(data.data.goods_attr.tbody).each(function($key, item) {
  								dIv += '<tr><td><input type="checkbox" class="inputc child_ck" attr_value="' + item.attr_value + '"  original_price="' + item.original_price + '" discount_price="' + item.discount_price + '" cdn="'+ data.data.cdn +'" attr_img="' + item.images + '"   inventory="' + item.inventory + '"  attribute_value_id="' + item.attribute_value_id + '" id="' + item.id + '"></td>'
  								$(item.val).each(function(j, itemm) {
  									dIv += '<td>' + itemm.value + '</td>'
  								})
  								dIv += '<td>' + item.discount_price + '</td>';
  								// dIv += '<td>' + item.one_price + '</td>';
  								dIv += '<td>' + item.original_price + '</td>';
  								dIv += '<td>' + item.inventory + '</td>';
  								dIv += '</tr>'
  							})
  							dIv+='</tbody>'
  							that.skuTbody.append(dIv);

  							$(".total").on("click",function(){
  								var is_checked=$(this).prop('checked');
  								if(is_checked){
  									$(".checkbox").parents(".choose-gd").find("input[type='checkbox']").prop("checked",true);
  								}else{
  									$(".checkbox").parents(".choose-gd").find("input[type='checkbox']").prop("checked",false);
  								}
  							});
  							$('.child_ck').on('click',function(){
  								var flag1=true;
  								$("#t_b input").each(function(i){
  									if($("#t_b input").eq(i).prop("checked")==false){
  										flag1=false;
  									}
  									$('.total').prop('checked',flag1==true?true:false)
  								})
  							})
  						}
  					}
  					);
  				})
  			},
  			submitActive:function(){ // 完成
  				var that = this;
  				$(".ok").on("click", function() {
  					var len = $(".step4 tr").length;
  					var tArray = new Array();
  					for (var i = 0; i < len; i++) {
  						var gd_info = [];
  						for (var j = 0; j < 8; j++) {
  							gd_info.push( $(".step4 tr").eq(i).find("input").eq(j).val() );
  						}
  						//  one_price: gd_info[3],
  						tArray.push({ attr_img: gd_info[1], collage_price: gd_info[2], original_price: gd_info[3], inventory: gd_info[4], attribute_value_id: gd_info[5], goods_attre_id: gd_info[6] });
  					};
  					$.post("/active/add", { active_attr: tArray, goods_id: that.id, active_name: that.at_name, active_num: that.join_num, begin_time: that.b_time, end_time: that.e_time },function success(data) {
  						if (data.code == 200) {
  							Utils.layer.msg(data.msg);
  							setTimeout(function(){
  								location.href="/active/index";
  							},1000)
  						} else {
  							Utils.layer.msg(data.msg);
  						}
  					});
  				})
  			},
  			showStepItem:function(step){
  				var stepNum = step;
  				if(step > 1){
  					stepNum = 2
  				}
  				$('.show_step_item').removeClass('active');
  				$('.show_step_item'+stepNum).addClass('active');
  				$('.layui-progress-bar').width(stepNum * 50 + '%').find('.layui-progress-text').text(stepNum+'/2')
  			},
  			showStep:function(){
  				$('.step').hide().siblings('.step'+this.step).show();
  				$('.ok').hide()
  				this.nextBtn.show()
  				this.prevBtn.show()
  				if(this.step == 1){
  					this.prevBtn.hide()
  				}else if(this.step == 2){
  					this.nextBtn.hide()
  				}else if(this.step == 4){
  					this.nextBtn.hide()
  					$('.ok').show()
  				}
  			},
  			init:function(){
  				laydate.render({
  					elem: '#bg_time'
  				});
  				laydate.render({
  					elem: '#ed_time'
  				});
  				$('.at_name').on('keyup', function() {
  					var num = $(this).val().length;
  					$('.changeNum').text(num)
  				})
  				this.showStep();
  				this.next();
  				this.prev();
  				this.search();
  				this.submitActive();
  			}

  		}
  		addActive.init()


  	});
}